package cn.itcast.service.impl;


import cn.itcast.dao.MemberDao;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import cn.itcast.utils.MD5Utils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //添加会员信息,并对密码加密
    @Override
    public void add(Member member) {
        if (member.getPassword() != null) {
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    //通过月份查询会员数量
    @Override
    public List<Integer> findConutByMonths(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";
            Integer count = memberDao.findCountBeforeMonth(date);
            memberCount.add(count);
        }
        return memberCount;
    }
    //查询所有套餐名称
    @Override
    public List<String> findSetmealName() {
        return memberDao.findSetmealName();
    }
    //查询套餐名称以及套餐预约的人数
    @Override
    public List<Map> findSetmealNameAndCount() {
        return memberDao.findSetmealNameAndCount();
    }
}
